package com.itheima.service.db.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.db.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.db.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    @Override
    public Long save(User user) {
        user.setPassword(SecureUtil.md5(user.getPassword()));
        //user.setCreated(new Date());
        //user.setUpdated(user.getCreated());

        userMapper.insert(user);

        //返回主键
        return user.getId();
    }

    @Override
    public User findByMobile(String mobile) {
        //条件查询对象
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getMobile,mobile);

        return userMapper.selectOne(qw);
    }

    @Override
    public void savePhone(String phone, Long id) {
        //条件查询对象
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getId,id);
        User user = userMapper.selectOne(qw);
        user.setMobile(phone);
        userMapper.updateById(user);
    }

    @Override
    public User findById(Long userId) {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getId,userId);
        return userMapper.selectOne(qw);

    }

    @Override
    public void updatePhone(Long userId, String phone) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,userId);
        User user = userMapper.selectOne(queryWrapper);
        user.setMobile(phone);
        userMapper.updateById(user);
    }
}
